package com.real8.romemp.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.real8.romemp.client.GameMessages;
import com.real8.romemp.client.place.LabourAllocationPlace.EMenuSelection;

public class LabourAllocationViewImpl extends Composite implements
		LabourAllocationView {

	private static LabourAllocationViewImplUiBinder uiBinder = GWT
			.create(LabourAllocationViewImplUiBinder.class);

	interface LabourAllocationViewImplUiBinder extends
			UiBinder<Widget, LabourAllocationViewImpl> {
	}

	private Presenter presenter;

	@UiField
	YearlyReportView yearlyReportView;

	@UiField
	HTMLPanel menuPanel;
	@UiField
	SpanElement unallocatedSpan;

	@UiField
	QuestionAndAnswerView allocationQAView;

	@UiField
	HTMLPanel donePanel;

	public LabourAllocationViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void setPresenter(Presenter aPresenter) {
		this.presenter = aPresenter;

		allocationQAView.setPresenter(new QuestionAndAnswerView.Presenter() {
			@Override
			public void onAnswer(int answer) {
				presenter.onAllocationAnswer(answer);
			}
		});
	}

	@Override
	public void hideAll() {
		yearlyReportView.setVisible(false);
		menuPanel.setVisible(false);
		allocationQAView.setVisible(false);
		donePanel.setVisible(false);
	}

	@Override
	public void showYearlyReport(int yearBC, int population, int armyUnits,
			int auxilia, int warships, int marines, int merchantShips,
			int merchants, int shipBuilders, int artisans, int freeLabour,
			int treasury, int foodReserve, int foodNeed) {
		yearlyReportView.showReport(yearBC, population, armyUnits, auxilia,
				warships, marines, merchantShips, merchants, shipBuilders,
				artisans, freeLabour, treasury, foodReserve, foodNeed);

		yearlyReportView.setVisible(true);
	}

	@Override
	public void showMenu(int unallocated) {
		unallocatedSpan.setInnerText("" + unallocated);

		menuPanel.setVisible(true);
	}

	@Override
	public void showAllocationQA(EMenuSelection selection) {
		GameMessages messages = (GameMessages) GWT.create(GameMessages.class);

		switch (selection) {
		case ARMY:
			allocationQAView.showQuestion(messages.toAllocateMilitary());
			break;
		case NAVY:
			allocationQAView.showQuestion(messages.toAllocateNavy());
			break;
		case MERCHANTS:
			allocationQAView.showQuestion(messages.toAllocateMerchants());
			break;
		case ARTISANS:
			allocationQAView.showQuestion(messages.toAllocateArtisans());
			break;
		case SHIP_BUILDERS:
			allocationQAView.showQuestion(messages.toAllocateShipBuilders());
			break;
		}
	}

	@UiHandler("armyButton")
	void onArmyButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onSelected(EMenuSelection.ARMY);
		}
	}

	@UiHandler("navyButton")
	void onNavyButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onSelected(EMenuSelection.NAVY);
		}
	}

	@UiHandler("merchantsButton")
	void onMerchantsButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onSelected(EMenuSelection.MERCHANTS);
		}
	}

	@UiHandler("artisansButton")
	void onArtisansButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onSelected(EMenuSelection.ARTISANS);
		}
	}

	@UiHandler("shipBuildersButton")
	void onShipBuildersButtonClicked(ClickEvent e) {
		if (presenter != null) {
			presenter.onSelected(EMenuSelection.SHIP_BUILDERS);
		}
	}

	@Override
	public void showFullyAllocated() {
		donePanel.setVisible(true);
	}
}
